package com.vegetable.modules.controller.baseData;

import com.vegetable.common.utils.R;
import com.vegetable.modules.service.baseData.impl.QiniuServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j
@RestController
@RequestMapping("/image")
@AllArgsConstructor
@Api(value = "图片", tags = "图片")
public class ImageController {
    @Autowired
    private QiniuServiceImpl qiniuService;

    @PostMapping("/upload")
    @ApiOperation(value = "上传图片", notes = "上传图片")
    public R uploadImage(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            throw new RuntimeException("上传文件不能为空");
        }
        // 上传到七牛云
        String url = qiniuService.upload(file, "agent");
        log.info("上传图片地址：{}", url);
        return R.data(url);
    }

    @PostMapping("/upload/product")
    @ApiOperation(value = "上传图片", notes = "上传图片")
    public R uploadProductImage(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            throw new RuntimeException("上传文件不能为空");
        }
        // 上传到七牛云
        String url = qiniuService.upload(file, "product");
        log.info("上传图片地址：{}", url);
        return R.data(url);
    }
}
